#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2021-12-2 13:59
# @Author : huabao.tan
# @File : objdect_key.py
# @Software: PyCharm
import datetime
import random
import tkinter as tk
import os
import xlwt
import xlrd
from tkinter import filedialog
from dateutil.parser import parse
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from tkinter.filedialog import askdirectory, askopenfilename
from openpyxl import load_workbook
from xlutils.copy import copy
from pypinyin import lazy_pinyin


class Name:

    def creat_name(self):
        first_name = random.choice('谭')
        head1 = random.randint(0xb0, 0xf7)
        body1 = random.randint(0xa1, 0xf9)
        vaa = f'{head1:x}{body1:x}'
        secound_name = bytes.fromhex(vaa).decode('gb2312')
        head2 = random.randint(0xb0, 0xf7)
        body2 = random.randint(0xb0, 0xf7)
        vab = f'{head2:x}{body2:x}'
        last_name = bytes.fromhex(vab).decode('gb2312')
        name = first_name + secound_name + last_name
        return name


class SpellName:

    def spell_name(self, name):
        a = ""
        b = lazy_pinyin(name)
        for c in b:
            a += c
        return a


class Phone:

    def creat_phone(self):
        sec = [3, 4, 5, 7, 8][random.randint(0, 4)]
        th = {
            3: random.randint(0, 9),
            4: [a for a in range(5, 10)][random.randint(0, 4)],
            5: [a for a in range(10) if a != 4][random.randint(0, 7)],
            7: [a for a in range(8)][random.randint(0, 7)],
            8: random.randint(0, 9)
        }[sec]
        end = random.randint(10000000, 99999999)
        phone = '1{0}{1}{2}'.format(sec, th, end)
        return phone


class Email(SpellName):

    def creat_email(self, name):
        n = SpellName.spell_name(self, name)
        list = ['@thb.com', '@mzt.com', '@talebase.cocm']
        acc = random.choice(list)
        c = ""
        for a in n:
            c += a
        email = c + acc
        return email


class SaveFile:

    def save_file(self):
        root = tk.Tk()
        root.withdraw()
        Fpath = filedialog.asksaveasfilename()
        return Fpath


class OpenFile:

    def open_file(self):
        root = tk.Tk()
        root.withdraw()
        Fpath = filedialog.askopenfilename()
        return Fpath


class Id:

    def creat_rand(self, n):
        if n < 10:   return '000' + str(n)
        if n < 100:  return '00' + str(n)
        if n < 1000: return '0' + str(n)
        return str(n)

    def creat_digit(self, m):
        if m < 10: return '0' + str(m)
        return str(m)

    def creat_id(self):
        acc = "452724"
        year = str(random.randint(1970, 2000))
        month = random.randint(1, 12)
        day = random.randint(1, 28)
        aca = random.randint(1, 9999)
        return acc + year + Id.creat_digit(self, month) + Id.creat_digit(self, day) + Id.creat_rand(self, aca)


class Time:

    def now_time(self):
        time = datetime.date.today()
        return time


class IdBirth(Id):

    def birthday(self, time):
        acc = str(parse(time[6:14]))[0:10]
        return acc


class Power:

    def creat_age(self):
        age = random.randint(18, 25)
        return str(age)

    def creat_sex(self):
        sex = random.choice('男女')
        return sex

    def education(self):
        list = ['初中及以下', '高中', '中专', '中技', '大专', '大学', '硕士研究生', '博士研究生']
        acc = random.choice(list)
        return acc

    def college(self):
        list = ['清华大学', '北京大学', '南京大学', '四川大学', '重庆大学', '西南大学', '浙江大学', '北京师范大学', '兰州大学', '东北大学']
        acc = random.choice(list)
        return acc

    def rank(self):
        list = ['正厅级', '副厅级', '正处级', '副处级', '正科级', '副科级', '三级职员', '四级职员']
        acc = random.choice(list)
        return acc

    def duty(self):
        list = ['正职', '副职']
        acc = random.choice(list)
        return acc

    def post(self):
        list = ['部门正职', '部门副职', '首席职员', '一级职员', '二级职员', '基层正职', '基层副职', '平台正职', '三级机构正职']
        acc = random.choice(list)
        return acc

    def politics_status(self):
        list = ['中共党员', '共青团员', '民革党员', '中国民主同盟', '民建会员', '中国农工民主党']
        acc = random.choice(list)
        return acc

    def foster(self):
        list = ['成熟', '可用', '关注']
        acc = random.choice(list)
        return acc

    def position(self):
        list = ['董事长,党委书记', '董事,党委副书记,副总经理', '党委委员,纪委书记', '副总经理,党委委员', '总会计师,党委委员', '副总经理,党委委员',
                '副总经理,党委委员', '总工程师', '一级顾问', '三级顾问', '三级顾问', '三级顾问', '总经理,党委副书记', '总经理,党委副书记', '副总经理,党委委员',
                '党委书记,副总经理', '总经理,党委副书记', '党委书记,副总经理', '总经理助理', '总经理助理']
        acc = random.choice(list)
        return acc

    def nation(self):
        list = ["蒙古族", "回族", "藏族", "苗族", "维吾尔族", "彝族", "壮族", "布依族",
                "白族", "朝鲜族", "侗族", "哈尼族", "哈萨克族", "满族", "土家族", "瑶族",
                "达斡尔族", "东乡族", "高山族", "景颇族", "柯尔克孜族", "拉祜族", "纳西族", "畲族",
                "傣族", "黎族", "傈僳族", "仫佬族", "羌族", "水族", "土族", "佤族",
                "阿昌族", "布朗族", "毛南族", "普米族", "撒拉族", "塔吉克族", "锡伯族", "仡佬族",
                "保安族", "德昂族", "俄罗斯族", "鄂温克族", "京族", "怒族", "乌孜别克族", "裕固族",
                "独龙族", "鄂伦春族", "赫哲族", "基诺族", "珞巴族", "门巴族", "塔塔尔族", "汉族"]
        acc = random.choice(list)
        return acc

    def province(self):
        list = [
            "山东", "江苏", "安徽", "浙江", "福建", "上海", "广东", "广西", "海南", "湖北", "湖南", "河南", "江西", "北京", "天津", "河北", "山西", "内蒙古",
            "宁夏", "新疆", "青海", "陕西", "甘肃", "四川", "云南", "贵州", "西藏", "重庆", "辽宁", "吉林", "黑龙江"
        ]
        acc = random.choice(list)
        return acc

    def randomtime(self, start, end, n, frmt='%Y-%m-%d'):
        stime = datetime.datetime.strptime(start, frmt)
        etime = datetime.datetime.strptime(end, frmt)
        time_datetime = [random.random() * (etime - stime) + stime for _ in range(n)]
        time_str = [t.strftime(frmt) for t in time_datetime]
        return time_str

    def type(self):
        list = ['现单位', '职数归属', '二级党委委员归属', '三级党委委员归属', '兼职单位']
        acc = random.choice(list)
        return acc

    def aff_tyoe(self):
        list = ['省公司职数', '国网职数', '低职高配', '改革遗留', '职员', '二线']
        acc = random.choice(list)
        return acc

    def unit(self):
        list = ["江苏电力", "助理副总师", "办公室", "发展部", "财务部", "安监部", "设备部", "营销部", "科技部", "建设部", "互联网部", "物资部", "宣传部",
                "审计部", "经法部", "组织部", "人资部", "离退休部", "后勤部", "党建部", "纪委办", "调控中心", "工会", "企协分会"]
        acc = random.choice(list)
        return acc

    def specialty(self):
        list = ["哲学", "经济学", "财政学", "金融学", "法学", "外交学", "社会学", "治安学", "教育学", "体育教育", "汉语言文学", "新闻学", "历史学", "信息与计算科学",
                "物理学", "应用化学", "地理科学"]
        acc = random.choice(list)
        return acc

    def degree(self):
        list = ['学士', '硕士', '博士']
        acc = random.choice(list)
        return acc

    def learning(self):
        list = ['函授', '全日制', '脱产', '在职', '自考']
        acc = random.choice(list)
        return acc

    def relation(self):
        list = ["妻子", "女儿", "父亲", "母亲", "哥哥", "儿子", "姐姐", "外甥女", "女婿", "丈夫", "妹妹", "弟弟", "儿媳", "岳父", "岳母", "姐夫", "妻哥",
                "嫂子",
                "内叔", "妻弟", "养女", "妹夫", "妻嫂", "妻兄", "连襟", "妹婿", "继母", "长女", "次女", "姨妈", "妻姐", "公公", "婆婆", "舅父", "伯父",
                "姑母"]
        acc = random.choice(list)
        return acc

    def other_type(self):
        list = ["退休", "离休", "已故", "内退", "二线"]
        acc = random.choice(list)
        return acc

    def nature(self):
        list = ['系统内', '系统外']
        acc = random.choice(list)
        return acc

    def exchange(self):
        list = ['是', '否']
        acc = random.choice(list)
        return acc


class UserMessage(Name, Id, Email, Phone, Time, Power):

    def creat_message(self):
        mes = "姓名:" + Name.creat_name(self) + "\n" + "身份证:" + Id.creat_id(self) + "\n" + "性别:" + Power.creat_sex(self) + \
              "\n" + "年龄:" + Power.creat_age(self) + "岁" + "\n" + "手机号码：" + Phone.creat_phone(self) + "\n" + "邮箱：" + \
              Email.creat_email(self, Name.creat_name(self)) + "\n" + "时间：" + str(Time.now_time(self)) + '\n'
        return mes


class CreatExcel(OpenFile):
    def __init__(self):
        self.filename = OpenFile.open_file(self)
        self.workbook = CreatExcel.open_excel(self)

    def open_excel(self):
        workbook = load_workbook(self.filename)
        return workbook

    def get_sheet_by_index(self, workbook, index):
        sheetnames = self.workbook.sheetnames
        sheet = self.workbook[sheetnames[index]]
        return sheet

    def sheet_row_col_value(self, index=None, row=None, col=None, value=None):
        # print( self.workbook)
        sheetnames = self.workbook.sheetnames
        sheet = self.workbook[sheetnames[index]]
        sheet.cell(row=row, column=col).value = value
        self.workbook.save(self.filename)
        return '成功写入'


if __name__ == '__main__':
    c = CreatExcel()
